package com.woniu40.petai_manager.web;

import com.github.pagehelper.PageInfo;
import com.woniu40.petai_manager.entity.Dynamic;
import com.woniu40.petai_manager.entity.User;
import com.woniu40.petai_manager.result.ResponseResult;
import com.woniu40.petai_manager.service.DynamicService;
import com.woniu40.petai_manager.vo.DynamicVo;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@CrossOrigin
@RequestMapping("dynamic")
public class DynamicController {

    @Autowired
    DynamicService dynamicService;


    @GetMapping("/findAll")
    @ApiOperation(value = "查询动态列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name ="name",required = false,dataType = "String"),
            @ApiImplicitParam(name ="pageNum",required = true,dataType = "Integer"),
            @ApiImplicitParam(name ="pageSize",required = true,dataType = "Integer")
    })
    public ResponseResult<PageInfo<DynamicVo>> findAll(String name,@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){
        PageInfo<DynamicVo> all = dynamicService.findAll(pageNum, pageSize,name);
        ResponseResult<PageInfo<DynamicVo>> result = new ResponseResult<>();
        result.setCode(200);
        result.setMessage("查询成功");
        result.setData(all);
        return result;
    }


    @PutMapping("/update")
    @ApiOperation(value = "更改动态文章状态")
    @ApiImplicitParam(name = "dynamicId",required = true,dataType = "Integer")
    public ResponseResult<String> update(Integer dynamicId){
        int priority = dynamicService.priority(dynamicId);
        ResponseResult<String> result = new ResponseResult<>();
        if(priority!=0){
            result.setCode(200);
            result.setMessage("更新成功");
        }else {
            result.setCode(108);
            result.setMessage("更新失败");
        }
        return result;
    }

    @DeleteMapping("/delete")
    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "dynamicId",required = true,dataType = "Integer")
    public ResponseResult<String> delete(Integer dynamicId){
        System.out.println(dynamicId);
        int delete = dynamicService.delete(dynamicId);
        ResponseResult<String> result = new ResponseResult<>();
        if(delete!=0){
            result.setCode(200);
            result.setMessage("删除成功");
        }else {
            result.setCode(107);
            result.setMessage("删除失败");
        }
            return result;

    }


}
